import axios from 'axios';
// import Cookies from 'js-cookie';
// import {url} from '../config/index.js';

export default function setAxios() {
  // axios.defaults.baseURL = url;
//   axios.defaults.headers.common['X-Token'] = Cookies.get('token');

  //修改默认的content-type
  // axios.defaults.headers.post['Content-Type'] = 'application/json;charse=UTF-8'

  return function fetch(url, method, cb, errcb, params) {
    if (method === 'get') { //get
      axios.get(url, {
          params,
        })
        .then((res) => {
          console.log('get success')
          console.log(res)
          cb(res);
        })
        .catch((err) => {
          console.log('get fail')
          errcb(err);
        });
    } else {    //post
      console.log(typeof params)
      axios.post(url, params)
        .then((res) => {
          console.log('post success')
          console.log(res)
          console.log(params)
          if (res.data.state === 0) {
            window.location.href = res.data.weixinUrl;
          } else {
            cb(res);
          }
        })
        .catch((err) => {
          console.log('post fail')
          errcb(err);
        });
    }
  };
}